﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product;
using ERP_RORZE_DLL.Product.database;

namespace ERP_RORZE_PRODUCT.Product.UserControls
{
    public partial class ctrlBusOderDeadline : DevExpress.XtraEditors.XtraUserControl
    {
        ClsBusOderDAO clsBusOderDAO = new ClsBusOderDAO();
        DataTable dt;
        ClsBusOderTable b = new ClsBusOderTable();
        ClsMOTable MO = new ClsMOTable();
        ClsMOOptionTable MOO = new ClsMOOptionTable();

        public ctrlBusOderDeadline()
        {
            InitializeComponent();
        }

        private void ctrlBusOderDeadline_Load(object sender, EventArgs e)
        {
            dt = clsBusOderDAO.GetAll();
            List<ClsBusOderTable> objs = new List<ClsBusOderTable>();
            foreach (DataRow r in dt.Rows)
            {
                ClsBusOderTable obj = new ClsBusOderTable();
                obj.BOderNo = r["BOderNo"].ToString();  //
                obj.PlanNo = r["planNo"].ToString();   ////
                obj.Dealine = (DateTime)r["Deadline"];
                obj.MONo = r["MONo"].ToString();     ////
                obj.Finished = (bool)r["Finished"];  ////
                obj.MOQty = (int)r["MOQty"];////
                obj.Qty = (int)r["Qty"]; /////
                objs.Add(obj);
            }
            cbBusOderNo.DataSource = objs;
            cbBusOderNo.DisplayMember = "BOderNo";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (checkFinish.Checked == true)
            {
                MessageBox.Show("Đơn hàng đã hoàn thành. Không thể thay đổi!");
            }
            else
            {
                ClsBusOderTable obj = new ClsBusOderTable();

                //biến truyền vào để Update vào BusOder
                obj.BOderNo = cbBusOderNo.Text;
                obj.Change = "Deadline Change";
                obj.Dealine = dateNewDeadline.Value;
                obj.ChangeDate = dateUpdatedate.Value;
                clsBusOderDAO.DeadlineBusOder(obj);

                //biến truyền vào để update MO
                MO.MONo = txtMONo.Text;
                MO.DeadLine = dateNewDeadline.Value;
                MO.Note = "Deadline Changed";
                clsBusOderDAO.DeadlineMO(MO);

                //truyền biến vào để Update MOOption
                MOO.MONo = txtMONo.Text;
                MOO.Deadline = dateNewDeadline.Value;
                clsBusOderDAO.DealineMOOption(MOO);

                ctrlBusOderDeadline_Load(sender, e);
            }

        }

        private void cbBusOderNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClsBusOderTable obj = ((ClsBusOderTable)cbBusOderNo.SelectedItem);
            txtPlanNo.Text = obj.PlanNo.ToString();
            txtMONo.Text = obj.MONo.ToString();
            txtMOQty.Text = obj.MOQty.ToString();
            txtOderQty.Text = obj.Qty.ToString();
            dateDeadline.Value = obj.Dealine;
            checkFinish.Checked = obj.Finished;
        }
    }
}
